package com.typhoon.spring_shiro.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**
 * 二维码编码工具类
 * 
 * @author Administrator
 *
 */
public class ZxingUtil {
	
	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;
	
    /**
     * 编码
     * 
     * @param contents
     * @param width
     * @param height
     * @param charset
     * @param hintType
     * @param outputStream
     * @throws IOException
     * @throws WriterException
     */
    public static void encode(String contents, int width, int height, String charset,
                    ErrorCorrectionLevel hintType, OutputStream outputStream) throws IOException,
                    WriterException {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
        // 指定纠错等级
        hints.put(EncodeHintType.ERROR_CORRECTION, hintType);
        // 指定编码格式
        hints.put(EncodeHintType.CHARACTER_SET, charset);
        // 二维码外边距去掉白边
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix =
                        new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width,
                                        height, hints);
        MatrixToImageWriter.writeToStream(bitMatrix, "png", outputStream);
    }

    /**
     * 编码
     * 
     * @param contents
     * @param width
     * @param height
     * @param outputStream
     * @throws WriterException
     * @throws IOException
     */
    public static void encode(String contents, int width, int height, OutputStream outputStream)
                    throws IOException, WriterException {
        encode(contents, width, height, "UTF-8", ErrorCorrectionLevel.M, outputStream);
    }
    
	/**
	 * 创建二维码并输出到指定文件
	 *
	 * @param matrix
	 * @param format
	 * @param file
	 * @throws IOException
	 * @date 2015年9月21日
	 * @author huangtao
	 */
	public void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
			}
		}
		if (!ImageIO.write(image, format, file)) {
			throw new IOException("Could not write an image of format " + format + " to " + file);
		}
	}
	
	/**
	 * 简易制作二维码
	 *
	 * @param content
	 * @param path
	 * @param name
	 * @throws WriterException
	 * @throws IOException
	 * @date 2015年9月22日
	 * @author huangtao
	 */
	public static void createQRBatch(String content,String path,String name) throws WriterException, IOException{

		MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
		Map<EncodeHintType,String> hints = new HashMap<EncodeHintType,String>();
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
		File file1 = new File(path,name+".png");
		if (!file1.exists()) {
			file1.mkdirs();
		}
		MatrixToImageWriter.writeToFile(bitMatrix, "png", file1);
	}
//	public static void main(String args[]){
//		try {
//            
//		     String content = "120605181003;http://www.cnblogs.com/jtmjx";
//		     String path = "C:/Users/Administrator/Desktop/testImage";
//		     
//		     MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
//		     
//		     Map hints = new HashMap();
//		     hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//		     BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400,hints);
//		     File file1 = new File(path,"餐巾纸.jpg");
//		     MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
//		     
//		 } catch (Exception e) {
//		     e.printStackTrace();
//		 }
//	}
	public static void main(String[] args) {
		System.out.println("ss");
	}
}
